package com.cxh.mediasessiondemo.classic_sample;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;

import androidx.annotation.Nullable;

import com.cxh.mediasessiondemo.ICallback;
import com.cxh.mediasessiondemo.IController;

/**
 * Created by Cxh
 * Time : 2021/10/31  02:14
 * Desc :  传统方式实现媒体播放，需要我们编写大量AIDL接口
 */
public class SampleMusicService extends Service {

    private MediaPlayer mMediaPlayer;
    private ICallback mCallback;

    @Override
    public void onCreate() {
        super.onCreate();
        mMediaPlayer = new MediaPlayer();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMediaPlayer.release();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mServerBinder;
    }

    private IController.Stub mServerBinder = new IController.Stub() {
        @Override
        public void play() throws RemoteException {
            // 播放逻辑
        }

        @Override
        public void pause() throws RemoteException {
            // 暂停逻辑
        }

        @Override
        public void register(ICallback cb) throws RemoteException {
            // 注册远程回调
            mCallback = cb;
        }

        @Override
        public void unregister(ICallback cb) throws RemoteException {
            // 释放远程回调
            mCallback = null;
        }
    };
}
